<%@page session="false" %>

<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.net.InetAddress"%>

<%@page import="com.googlecode.afdbaccess.web.common.util.IncludeChecker"%>
<%@page import="com.googlecode.afdbaccess.web.common.util.HtmlEscaper"%>
<%@page import="com.googlecode.afdbaccess.web.common.errorhelp.StacktraceToHtml"%>

<%!
  String getParameterAndFilterNullString(HttpServletRequest request, String parameterName) {
    String value = request.getParameter(parameterName);
    
    if(value != null && value.equals("null") == false) {
        return value;
    } else {
        return null;
    }
  }
%>

<%
  response.setDateHeader("Expires", 0);
  response.setHeader("Cache-control", "must-revalidate");
  IncludeChecker.demandIncluded(request);

  String form_pagetitle = getParameterAndFilterNullString(request, "pagetitle");
  String form_header = getParameterAndFilterNullString(request, "header");
  String form_message = getParameterAndFilterNullString(request, "message");
  String form_buttonUrl = getParameterAndFilterNullString(request, "buttonurl");
  String form_buttonTitle = getParameterAndFilterNullString(request, "buttontitle");
  String form_showBackbutton = getParameterAndFilterNullString(request, "showbackbutton");
  String form_stacktrace = getParameterAndFilterNullString(request, "stacktrace");
  String form_hidestacktrace = getParameterAndFilterNullString(request, "hidestacktrace");
  String form_uniqueId = getParameterAndFilterNullString(request, "uniqueid");
  String form_error = getParameterAndFilterNullString(request, "error");
  
  boolean error = false;
  if(form_error != null && (form_error.trim().equalsIgnoreCase("yes") || form_error.trim().equalsIgnoreCase("true"))) {
      error = true;
  }
  
  String defaultTitle = "Error message";
  String defaultMessage = "Unrecognized error.";
  if(error == false) {
      defaultTitle = "Message";
      defaultMessage = "";
  }

  String pagetitle = defaultTitle;
  if(form_pagetitle != null && form_pagetitle.equals("") == false) {
    pagetitle = form_pagetitle;
  }

  String header = defaultTitle;
  if(form_header != null && form_header.equals("") == false) {
    header = form_header;
  }

  String message = defaultMessage;
  if(form_message != null && form_message.equals("") == false) {
    message = form_message;
  }
  
  String stacktraceString = form_stacktrace;
  boolean hideStacktrace = form_hidestacktrace != null && (form_hidestacktrace.trim().equals("yes") || form_hidestacktrace.trim().equals("true"));
  
  String uniqueId = form_uniqueId;
  
  boolean addButton = form_showBackbutton != null && (form_showBackbutton.trim().equals("yes") || form_showBackbutton.trim().equals("true"));
  
  String buttonUrl = "javascript:history.go(-1)";
  if(form_buttonUrl != null && form_buttonUrl.trim().equals("") == false) {
    buttonUrl = HtmlEscaper.escapeHtml(form_buttonUrl);
    addButton = true;
  }

  String buttonTitle = " Back ";
  if(form_buttonTitle != null && form_buttonTitle.trim().equals("") == false) {
    buttonTitle = HtmlEscaper.escapeHtml(form_buttonTitle);
    addButton = true;
  }
%>
<jsp:include page="/include/header.jspf">
  <jsp:param name="title" value="<%= pagetitle %>"/>
</jsp:include>
<%
  if(header != null && header.equals("") == false) {
      String headerClass = "";
      if(error == true) {
          headerClass = " class=\"errorheader\"";
      }
%>
<div class="contentTop">
<h1<%= headerClass %>><%= HtmlEscaper.escapeHtml(header) %></h1>
</div>
<%
  }
%>
<div class="contentMiddle">
<%
  if(message != null && message.trim().equals("") == false) {
%>
<p>
<%= HtmlEscaper.escapeHtml(message) %>
<%
  }
  
  if(addButton) {
%>
<p>
<a href="<%= buttonUrl %>"><%= buttonTitle %></a>
<%
  }

  if(stacktraceString != null && stacktraceString.trim().equals("") == false) {
    DateFormat df = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.S");
  
    String uniqueIdString = "";
    if(uniqueId != null && uniqueId.trim().equals("") == false) {
        uniqueIdString = " (" + HtmlEscaper.escapeHtml(uniqueId) + ")";
    }
    
    InetAddress localAddress = java.net.InetAddress.getLocalHost();
    
    String htmlInfo = "";
    htmlInfo += df.format(new Date()) + uniqueIdString + ": #REQUESTURL#\n";
    htmlInfo += "Host: " + localAddress.getHostAddress() + " = " + localAddress.getCanonicalHostName() + "\n";
    htmlInfo += "\n";
    htmlInfo += StacktraceToHtml.makeHtmlFromStacktraceString(stacktraceString.trim());
    
    if(hideStacktrace) {
%>
<a id="show-extended-info-button" style="cursor: pointer" onclick="document.getElementById('extended-info').style.display=''; document.getElementById('show-extended-info-button').style.display='none'">&nbsp;&nbsp;</a>
<div id="extended-info" style="display: none">
<%
    }
%>
<p style="margin-top: 30px">
<b>Extended error information:</b>
<p>
<div id="clipboardpre" style="border: 1px solid #E0E0E0">
  <pre><%= htmlInfo.trim() %></pre>
</div>
<script type="text/javascript">
  var node = document.getElementById("clipboardpre");
  
  node.innerHTML = node.innerHTML.replace('#REQUESTURL#', document.location);
</script>
<%
    if(hideStacktrace) {
%>
</div>
<%
    }
  }
%>
</div>
<jsp:include page="/include/footer.jspf"/>